<template>
  <div class="main">
    <Navigation ref="navigation"></Navigation>

    <personInfo @onreload="onreload"
                v-if="showPersonInfo"
                key="personInfo"
                ref="personInfo"></personInfo>

    <SwitchAccount v-if="showSwitchAccount"
                   ref="switchAccount"></SwitchAccount>

    <personal @login_success="login_success"
              ref="personal"
              v-show="showPersonal"
              key="personal"
              :personalBg="personalBg"></personal>

    <modifyPhone ref="modifyPhone"
                 @close="close"
                 v-if="showModifyPhone">
    </modifyPhone>

    <msg :msgValue="msg"></msg>
  </div>
</template>

<script>
  import Navigation from "@/components/Navigation";
  import personal from "@/components/personal/personal";
  import personInfo from "@/components/personal/personInfo";
  import SwitchAccount from '@/components/personal/switchAccount'
  import msg from "@/components/msg";
  import modifyPhone from "@/components/personal/modifyPhone";
  import mixinCommon from "@/assets/js/mixin-common"

  export default {
    name: "personalIndex",
    components: {
      Navigation,
      personal,
      personInfo,
      SwitchAccount,
      msg,
      modifyPhone
    },
    mixins:[mixinCommon],
    inject: ["reload"],
    watch: {
      logined: function (n, o) {
        if (n) {
          this.personalBg = require("@/assets/images/personal/personal_bg.png")  //登录的背景图
        } else {
          this.personalBg = null
        }
      }
    },
    data() {
      return {
        msg: {},
        personalBg: null,
        showPersonal: false,
        showPersonInfo: false,
        showSwitchAccount: false,
        logined: false,
        showModifyPhone: false
      };
    },
    methods: {
      onreload() {
        this.showPersonal = true
        this.showPersonInfo = false
        this.reload()
      },
      login_success() {
        this.showPersonal = false
        if (this.showPersonInfo === false) {
          this.logined = true
          this.showPersonInfo = true
          setTimeout(() => {
            this.hs.initFocus('0-0')
          }, 25)
        } else if (this.showSwitchAccount === true) {
          this.$refs.switchAccount.getStudentList()
          setTimeout(() => {
            this.hs.initFocus('110-0')
          }, 25)
        }
      },
      closePerson() {
        this.showPersonal = false
        // 初始化定位焦点
        if (this.logined) {
          setTimeout(() => {
            this.hs.initFocus('110-0')
          }, 15)
        }
      },
      goHomePage() {
        this.$bridge.callHandler(
          "wasu_function_gotoApplication",
          {
            packageName: "com.wasu.launcher",
            className: "com.wasu.launcher.MainActivity"
          },
          function (response) {
          }
        );
      },
      close(obj) {
        if (obj.name === 'modifyPhone') {
          // 更新信息
          this.$refs.personal.getStudent()
          this.showModifyPhone = false
          this.hs.initFocus('2-0', true)
        }
      }
    },
    created() {
      this.wasuLog(this.$route.path, '个人中心')
    },
    mounted() {
      // 导航滚动
      this.$refs.navigation.$refs.scrollContainer.scrollBy(-560, true);

      // console.log(this.$refs.personal);
      // 获取当前登录账号
      if (this.$refs.personal.getStudent()) {
        this.logined = true
      }

      this.hs.initFocus('100-6')
      // 有登录状态的时候隐藏
      if (Object.keys(this.$store.state.user).length) {
        this.showPersonal = false
        this.showPersonInfo = true
      } else {
        this.showPersonal = true
        this.showPersonInfo = false
      }

      // 返回事件
      this.bridge.whenKeyBack = () => {
        if (this.hs.focObj.group === 103) { // 修改手机号
          // this.$set(this.changeModifyPhone, 'delete', true)
          this.$refs.modifyPhone.whenBack()
        } else if (this.hs.focObj.group === 106) {           // 当前路径, 涉及到登录框时, 注意是首页登录还是新增账户
          if (this.hs.focObj.objindex === 0) {  // 输入框1
            let inputValue1 = this.$refs.personal.inputValue1
            // 当为新增用户时
            if (this.logined && !inputValue1.length) {
              this.closePerson()
              return
            }
            this.$refs.personal.inputValue1 = inputValue1.substring(
              0,
              inputValue1.length - 1
            );
          } else if (this.hs.focObj.objindex === 1) {  // 输入框2
            let inputValue2 = this.$refs.personal.inputValue2
            // 当为新增用户时
            if (this.logined && !inputValue2.length) {
              this.closePerson()
              return
            }
            this.$refs.personal.inputValue2 = inputValue2.substring(
              0,
              inputValue2.length - 1
            );
          } else {    // 确定键
            if (this.logined) { // 当为新增用户时
              this.closePerson()
            } else {
              this.goHomePage()
            }
          }
        } else if (this.hs.focObj.group === 105) {  // 登录方式选择框
          if (this.logined) {  // 当为新增用户时
            this.closePerson()
          } else {
            this.goHomePage()
          }
        } else if (this.hs.focObj.group === 110) {
          // 关闭
          this.$refs.personInfo.closeSwitchAccount()
        } else {
          this.goHomePage()
        }
      };

      // test
      // setTimeout(()=>{ // test modifyPhone
      //   this.showModifyPhone = true
      // },2000)

    }
  }
  ;
</script>
<style scoped lang="less">
  .main {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 1920px;
    height: 1080px;
  }
</style>
